#include <iostream>
#include <stdlib.h>

#include "gtest/gtest.h"

#include "kpextVkContext.h"
#include "KpExtGP.h"
#include "kpextOpImgCvtU16C1toF32C1.h"

#include "kpextCtxGlobal.h"

using namespace std;

TEST(kpextFuncTest, OpImgCvtU16C1toF32C1){
    ASSERT_NE(nullptr, GvkContextPtr);

    int cols = 2048, rows = 1000;

    auto cmdbuf = GvkContextPtr->cmdbuf();

    size_t size = static_cast<size_t>(cols * rows);
    auto stagebuf1 = GvkContextPtr->stagingBuf(size * 2 + 4);
    auto stagebuf2 = GvkContextPtr->stagingBuf(size * 4 + 4);
    auto inbuf = GvkContextPtr->deviceBuf(size * 2 + 4);
    auto outbuf = GvkContextPtr->deviceBuf(size * 4 + 4);
    auto op = std::make_shared<kpext::OpImgCvtU16C1toF32C1>(
                GvkContextPtr
                );

    ASSERT_NE(nullptr, cmdbuf);
    ASSERT_NE(nullptr, stagebuf1);
    ASSERT_NE(nullptr, stagebuf2);
    ASSERT_NE(nullptr, inbuf);
    ASSERT_NE(nullptr, outbuf);
    ASSERT_NE(nullptr, op);

    {
        auto data = stagebuf1->data<uint16_t>();
        for (size_t i = 0; i < size; ++i) {
            data[i] = static_cast<uint16_t>(i);
        }
        stagebuf1->unmap();
    }

    kpext::CmdBaseBufferCpy bufcpy;
    kpext::CmdBaseBufferSet bufset;
    kpext::CmdBuffersMemBarrier barrierTx;
    kpext::CmdBuffersMemBarrier barrierRx;

    barrierTx.prepare(
                {inbuf, outbuf},
                vk::PipelineStageFlagBits::eTransfer,
                vk::PipelineStageFlagBits::eComputeShader,
                vk::AccessFlagBits::eTransferWrite,
                vk::AccessFlagBits::eShaderRead
                );
    barrierRx.prepare(
                {inbuf, outbuf},
                vk::PipelineStageFlagBits::eComputeShader,
                vk::PipelineStageFlagBits::eTransfer,
                vk::AccessFlagBits::eShaderWrite,
                vk::AccessFlagBits::eTransferRead
                );

    op->rebuild(cols, rows,
                inbuf, outbuf
                );

    cmdbuf->begin();
    bufcpy.prepare(inbuf, stagebuf1);
    cmdbuf->record(&bufcpy);
    cmdbuf->record(&barrierTx);

    cmdbuf->record(op.get());

    bufcpy.prepare(stagebuf2, outbuf);
    cmdbuf->record(&barrierRx);
    cmdbuf->record(&bufcpy);

    cmdbuf->submit();
    cmdbuf->waitExec();

    {
        auto p1 = stagebuf1->data<uint16_t>();
        auto p2 = stagebuf2->data<float>();
//            SPDLOG_INFO(__FUNCTION__ " output: [{}] ", stagebuf2->vector<float>());
        bool isalleq = true;

        for (size_t i = 0; i < size; ++i) {
            isalleq = isalleq &&
                    (p1[i] == static_cast<uint16_t>(p2[i]));
        }

        stagebuf1->unmap();
        stagebuf2->unmap();
        EXPECT_EQ(true, isalleq);
    }

}

GTEST_API_ int main(int argc, char **argv) {
    printf("Running main() from %s\n", __FILE__);
    //    kpextSetLoggerLevel(KpExtLOG_LEVEL_DEBUG);
    kpextSetLoggerLevel(KpExtLOG_LEVEL_INFO);
    //  kpextSetLoggerPattern("[%D %H:%M:%S.%e] [%^%l%$] %v");
    kpextSetLoggerPattern("   [SPDLOG_%l] %v");

    testing::InitGoogleTest(&argc, argv);

    testing::Environment* const foo_env =
            testing::AddGlobalTestEnvironment(new KpExtCtxEnv);
    UNUSE_VAR(foo_env);

    return RUN_ALL_TESTS();
}
